package com.senlingspace.web.controller.weixin;

import com.alibaba.fastjson2.JSON;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.request.WxLoginCodeRequest;
import com.senlingspace.framework.security.context.SecurityUtilExt;
import com.senlingspace.system.service.impl.MiniAppAuthService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 微信小程序 认证相关接口
 *
 * @author lebronjames
 * @date 2024/09/24
 */
@Slf4j
@Validated
@AllArgsConstructor
@RestController
@RequestMapping(path = "/wmp/auth/v1")
public class MiniAppAuthController {

    private final MiniAppAuthService miniAppAuthService;


    /**
     * 登录
     *
     * @param request
     * @return {@link AjaxResult }
     */
    @PostMapping(path = "/login")
    public AjaxResult login(@Valid @RequestBody WxLoginCodeRequest request) {
        log.info("微信小程序，用户登录：{}", JSON.toJSONString(request));
        return miniAppAuthService.login(request);
    }

    /**
     * 退出
     *
     * @return {@link AjaxResult }
     */
    @PostMapping(path = "/logout")
    public AjaxResult logout() {
        log.info("微信小程序退出登录");
        return miniAppAuthService.logout(SecurityUtilExt.getMobile());
    }

}
